package org.thesis.dataAccess;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts;
import android.provider.Contacts.People;
import android.provider.ContactsContract;
import android.util.Log;

public class db_econtact //lay du lieu tu contact dien thoai
{
	Cursor mCursor;
	String tag = "db_econtact";
	String email;
	Context context;
	
	ContentResolver cr;
	
	public db_econtact(Context c) {
//		super();
		context = c;
		cr = context.getContentResolver();
		// TODO Auto-generated constructor stub
	}

	public Cursor  get_email_from_contact()
	{
		try {
			mCursor = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);
//			if(mCursor.moveToFirst())
//			{
//				do
//				{
//					email = mCursor.getString(mCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
//					Log.v(tag, email);
//					
//				}while(mCursor.moveToNext());
//			}
			
		} catch (Exception e) {
			// TODO: handle exception
			Log.v(tag, "loi get email");
		}
		return mCursor;
	}
	
	public void add_econtact(String name, String email)
	{
		try
		{
			ContentValues values = new ContentValues();
//			Uri emailUri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY);
//			cv.put(ContactsContract.Contacts.DISPLAY_NAME, name);
//			cv.put(ContactsContract.CommonDataKinds.Email.DATA, email);
//			Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
//			cr.insert(uri, cv);
			
			values.put(People.NAME, name);
			// 1 = the new contact is added to favorites
			// 0 = the new contact is not added to favorites
			values.put(People.STARRED, 1);

			Uri uri = cr.insert(People.CONTENT_URI, values);
			
			Uri phoneUri = null;
			Uri emailUri = null;

			// Add a phone number for Abraham Lincoln.  Begin with the URI for
			// the new record just returned by insert(); it ends with the _ID
			// of the new record, so we don't have to add the ID ourselves.
			// Then append the designation for the phone table to this URI,
			// and use the resulting URI to insert the phone number.
			phoneUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);

			values.clear();
			values.put(People.Phones.TYPE, People.Phones.TYPE_MOBILE);
			values.put(People.Phones.NUMBER, "");
			cr.insert(phoneUri, values);

			// Now add an email address in the same way.
			emailUri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY);

			values.clear();
			// ContactMethods.KIND is used to distinguish different kinds of
			// contact methods, such as email, IM, etc. 
			values.put(People.ContactMethods.KIND, Contacts.KIND_EMAIL);
			values.put(People.ContactMethods.DATA, email);
			values.put(People.ContactMethods.TYPE, People.ContactMethods.TYPE_HOME);
			cr.insert(emailUri, values);
			Log.v(name, "insert thanh cong");
		}catch (Exception e) {
			// TODO: handle exception
			Log.v(name, "Loi add " +e.toString());
		}
	}
	
}
